package com.example.mo.allfind.ui.persional;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mo.allfind.R;
import com.example.mo.allfind.ui.PersionalActivity;
import com.example.mo.allfind.util.WheelView;

import java.util.Arrays;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyInfoActivity extends AppCompatActivity {

    @Bind(R.id.common_title_bar_left)
    TextView commonTitleBarLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.modify_et_name)
    EditText modifyEtName;
    @Bind(R.id.modify_tv_sex)
    TextView modifyTvSex;
    @Bind(R.id.modify_tv_birthday)
    TextView modifyTvBirthday;
    @Bind(R.id.modify_btn_confirm)
    Button modifyBtnConfirm;

    String data = "1995-10-13";
    final int DATE_DIALOG = 1;
    int mYear, mMonth, mDay;
    private static final String[] PLANETS = new String[]{"男", "女"};
    Intent intent ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_info);
        ButterKnife.bind(this);
        commonTitleBarLeft.setVisibility(View.VISIBLE);
        intent = new Intent(ModifyInfoActivity.this, PersionalActivity.class);
        tvTitle.setText("修改个人信息");
        String name = getIntent().getStringExtra("name");
        String[] str = name.split(":");
        modifyEtName.setText(str[str.length-1]);

        String aa[] = data.split("\\-");
        mYear = Integer.parseInt(aa[0]);
        mMonth = Integer.parseInt(aa[1]);
        mDay = Integer.parseInt(aa[2]);
    }

    @OnClick({R.id.common_title_bar_left,R.id.modify_tv_birthday,R.id.modify_tv_sex,R.id.modify_btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.common_title_bar_left:
                intent.putExtra("name",modifyEtName.getText().toString().trim());
                intent.putExtra("sex",modifyTvSex.getText().toString().toString());
                setResult(10,intent);
                finish();
                break;
            case R.id.modify_tv_birthday:
                showDialog(DATE_DIALOG);
                break;
            case R.id.modify_tv_sex:
                final int i ;
                final String[] sex = new String[1];
                if(modifyTvSex.getText().equals("男")){
                    i = 0;
                } else {
                    i = 1;
                }
                final View outerView = LayoutInflater.from(this).inflate(R.layout.wheel_view, null);
                WheelView wv = (WheelView) outerView.findViewById(R.id.wheel_view_wv);
                wv.setOffset(1);
                wv.setItems(Arrays.asList(PLANETS));
                wv.setSeletion(i);
                wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
                    @Override
                    public void onSelected(int selectedIndex, String item) {
                        sex[0] = item.toString();
                    }
                });
                new AlertDialog.Builder(this)
                        .setTitle("性别")
                        .setView(outerView)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                modifyTvSex.setText(sex[0]);
                            }
                        })
                        .show();
                break;
            case R.id.modify_btn_confirm:
                intent.putExtra("name",modifyEtName.getText().toString().trim());
                intent.putExtra("sex",modifyTvSex.getText().toString().toString());
                setResult(10,intent);
                finish();
                break;
        }

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }

    /**
     * 设置日期 利用StringBuffer追加
     */
    public void display() {
        modifyTvBirthday.setText(new StringBuffer().append(mYear).append("-").append(mMonth+1).append("-").append(mDay).append(" "));
    }

    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };

    @Override
    public void onBackPressed() {
//        Intent intent = new Intent(ModifyInfoActivity.this, PersionalActivity.class);
//        intent.putExtra("name",modifyEtName.getText().toString().trim());
//        intent.putExtra("sex",modifyTvSex.getText().toString().toString());
//        setResult(10,intent);
        super.onBackPressed();
    }
}
